package com.lkl.demo.provider.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lkl.demo.common.constant.RabbitMQConstant;
import com.lkl.demo.provider.bean.Orders;
import com.lkl.demo.provider.mapper.OrdersMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RabbitListener(queues = RabbitMQConstant.DEAD_QUEUE)
public class OrderCheckHandler {

    @Autowired
    private OrdersMapper ordersMapper;

    @RabbitHandler
    public void orderHandler(Map<String, String> map) {
        System.out.println("死信队列处理，接收消息" + map);

        String orderId = map.get("orderId");
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("id", orderId);

        Orders orders = ordersMapper.selectOne(wrapper);
        if (null != orders) {

            if ("0".equals(orders.getType())) {
                orders.setType("2");
                ordersMapper.updateById(orders);
            }

            System.out.println("订单已取消");
        } else {
            System.out.println("该订单已支付");
        }
    }

}
